package com.sjft.cloud.commons.core.utils;

import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * DbUtils
 *
 * @author Jin
 */
@Slf4j
public class DbUtils {


    private final DataSource dataSource;

    private DbUtils(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public static DbUtils use(DataSource dataSource) {
        return new DbUtils(dataSource);
    }

    public List<Map> querySql(String sql){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            conn = dataSource.getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs!=null){
                return convertList(rs);
            }
        }catch(Exception e){
            log.error("sql 执行失败！ -{}", e);
        }finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    log.error("ResultSet 关闭失败！ -{}", e);
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    log.error("PreparedStatement 关闭失败！ -{}", e);
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("connection 关闭失败！ -{}", e);
                }
            }
        }
        return null;
    }
    private static List<Map> convertList(ResultSet rs) throws SQLException {
        List<Map> list = new ArrayList<>();
        ResultSetMetaData md = rs.getMetaData();//获取键名
        int columnCount = md.getColumnCount();//获取列的数量
        while (rs.next()) {
            Map<String, Object> rowData = new HashMap<>();//声明Map
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));//获取键名及值
            }
            list.add(rowData);
        }
        return list;
    }

    public boolean exec(String sql) {
        Connection conn = null;
        Statement stmt = null;
        try {
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            return stmt.execute(sql);
        } catch (SQLException e) {
            log.error("sql 执行失败！ -{}", e);
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    log.error("connection 关闭失败！ -{}", e);
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    log.error("statement 关闭失败！ -{}", e);
                }
            }
        }
    }

}
